#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\time\calendar.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

#undef HANDLE
#undef ABSTRACT
#define STRUCT
namespace Cephei { namespace QL { namespace Times {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ICalendar
	public ref class CCalendar  : 
            public Cephei::QL::Times::ICalendar
	{
	protected: 
		boost::shared_ptr<QuantLib::Calendar>* _ppCalendar;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Calendar>* _phCalendar;
#endif
		Object^ _CalendarOwner;     // reference to object that manages the storage for this object
	internal:
		CCalendar ();
        CCalendar (boost::shared_ptr<QuantLib::Calendar>& childNative, Object^ owner);
        CCalendar (QuantLib::Calendar& childNative, Object^ owner);
        CCalendar (CCalendar^ copy);
        CCalendar (PLATFORM::Type^ t);
#ifdef STRUCT
        CCalendar (QuantLib::Calendar childNative);
#endif       
#ifdef HANDLE
		CCalendar (QuantLib::Handle<QuantLib::Calendar>& childNative, Object^ owner);
		CCalendar (QuantLib::Handle<QuantLib::Calendar> childNative);
#endif
		virtual ~CCalendar ();
		!CCalendar ();

	internal:
		QuantLib::Calendar& GetReference ();
		boost::shared_ptr<QuantLib::Calendar>& GetShared ();
		QuantLib::Calendar* GetPointer ();
        void SetCalendar (boost::shared_ptr<QuantLib::Calendar> native)
        {
            if (_ppCalendar != NULL)
                delete _ppCalendar;
            _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Calendar>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
		virtual Cephei::QL::Times::ICalendar^ AddHoliday (DateTime d) ;
		virtual DateTime Adjust (DateTime d, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ convention) ;
		virtual DateTime Advance (DateTime date, Cephei::QL::Times::IPeriod^ period, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ convention, Microsoft::FSharp::Core::FSharpOption<Boolean>^ endOfMonth) ;
		virtual DateTime Advance (DateTime d, Int32 n, QL::Times::TimeUnitEnum unit, Microsoft::FSharp::Core::FSharpOption<QL::Times::BusinessDayConventionEnum>^ convention, Microsoft::FSharp::Core::FSharpOption<Boolean>^ endOfMonth) ;
		virtual Int64 BusinessDaysBetween (DateTime from, DateTime to, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeFirst, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeLast) ;
        property Boolean Empty 
        {
		    virtual Boolean get () ;
        }
		virtual DateTime EndOfMonth (DateTime d) ;
		virtual Cephei::Core::IVector<DateTime>^ HolidayList (Cephei::QL::Times::ICalendar^ calendar, DateTime from, DateTime to, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeWeekEnds) ;
		virtual Boolean IsBusinessDay (DateTime d) ;
		virtual Boolean IsEndOfMonth (DateTime d) ;
		virtual Boolean IsHoliday (DateTime d) ;
		virtual Boolean IsWeekend (QL::Times::WeekdayEnum w) ;
        property String^ Name 
        {
		    virtual String^ get () ;
        }
		virtual Cephei::QL::Times::ICalendar^ RemoveHoliday (DateTime d) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Times::ICalendar^>::typeid)]
	[FactoryFor(Cephei::QL::Times::ICalendar::typeid)]
	[FactoryFor(Cephei::QL::Times::ICalendar_Factory::typeid)]
	public ref class CCalendar_Factory sealed : public ICalendar_Factory
	{
	public:
        virtual ICalendar^ Create ();
    };
   
/*Cephei*/ } /*QL*/ } /*Times */}
